﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Calendar;
using Jzy.Model.Calendar;

using System.Collections.Generic;
using Jzy.Model;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Dept;
using Frame.Lib.Core.User;


public partial class Calendar_WeekList : PageList
{
    private CalendarBO cal = new CalendarBO();
    private DateTime dTmp;
    DataView dv;

    private CalendarLevel CalLevel
    {
        get
        {
            return (CalendarLevel)int.Parse(Request["Level"]);
        }
    }

    private DateTime CurrentTime
    {
        get
        {
            if (ViewState["CurrentTime"] == null)
            {
                ViewState["CurrentTime"] = DateTime.Now;
            }
            return (DateTime)ViewState["CurrentTime"];
        }
        set
        {
            ViewState["CurrentTime"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CalendarBO.DeptUserSelect drpDeptUser = new CalendarBO.DeptUserSelect(drpDepts, drpUsers);
            drpDeptUser.Bind(CalLevel);

            if (Request["DateTime"] != null)
            {
                CurrentTime = DateTime.Parse(Request["DateTime"]);
            }

            BindView();
        }
        AddAttr();
        JSUtil.SetDivHeight("list", 70); 
    }

    protected string Truncate(object strName,int length)
    {
        string str = StrUtil.Truncate(strName.ToString(), length);
        if (str.Length < strName.ToString().Length)
        {
            str += "…";
        }
        return str;
    }

    private void AddAttr()
    {
        lnkAdd.Attributes.Add("onclick", PopForm(true,""));
        lnkMonthCal.HRef = "MonthList.aspx?DateTime=" + System.Web.HttpUtility.UrlEncode(CurrentTime.ToString()) + "&Level=" + (int)CalLevel;
    }

    private void BindView()
    {
        dv = cal.GetCurrentView(CalLevel, CurrentTime, drpDepts, drpUsers).DefaultView;

        DataTable dt = cal.CreateWeek(CurrentTime);

        lv.DataSource = dt.DefaultView;       
        lv.DataBind();

        DateTime monday = DateTime.Now.GetMonday(CurrentTime);
        lblStartTime.Text = monday.ToString("M月d日");
        lblEndTime.Text = monday.AddDays(6).ToString("M月d日");
    }
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView drv = (DataRowView)item.DataItem;
            dTmp = (DateTime)drv["Date"];

            if (dTmp == DateTime.Today)
            {
                HtmlControl trdays = (HtmlControl)e.Item.FindControl("trdays");
                trdays.Attributes.Add("class","cal_currday");
            }

            ListView lvAM = (ListView)e.Item.FindControl("lvAM");
            lvAM.ItemDataBound += new EventHandler<ListViewItemEventArgs>(rpt_ItemDataBound);
            ListView lvPM = (ListView)e.Item.FindControl("lvPM");
            lvPM.ItemDataBound += new EventHandler<ListViewItemEventArgs>(rpt_ItemDataBound);

            dv.RowFilter = "EndTime>='" + dTmp.ToString() + "' and StartTime<='" + dTmp.AddHours(12).ToString() + "'";
            lvAM.DataSource = dv;
            lvAM.DataBind();

            dv.RowFilter = "EndTime>'" + dTmp.AddHours(12).ToString() + "' and StartTime<='" + dTmp.AddHours(24).ToString() + "'";
            lvPM.DataSource = dv;
            lvPM.DataBind();

            HtmlControl tdAM = (HtmlControl)e.Item.FindControl("tdAM");
            tdAM.Attributes.Add("ondblclick", PopForm(true, "?StartTime=" + Server.UrlEncode(dTmp.AddHours(9).ToString("yyyy-MM-dd HH:mm:ss"))));
            HtmlControl tdPM = (HtmlControl)e.Item.FindControl("tdPM");
            tdPM.Attributes.Add("ondblclick", PopForm(true, "?StartTime=" + Server.UrlEncode(dTmp.AddHours(13).ToString("yyyy-MM-dd HH:mm:ss"))));
        }       
    }

   

    void rpt_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            //e.Item.Style.Add("border-width", "0");
           // HiddenField hidStartTime = (HiddenField)e.Item.FindControl("hidStartTime");
            Label lblTime = (Label)e.Item.FindControl("lblTime");
            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView drv = (DataRowView)item.DataItem;
            DateTime dStartTime = (DateTime)drv["StartTime"];

            if (((ListView)sender).ID == "lvAM")
            {
                if (dStartTime > dTmp && dStartTime < dTmp.AddHours(12))
                {
                    string strStartTime = dStartTime.ToShortTimeString();
                    lblTime.Text = "§" + strStartTime + "";
                }
                else
                {
                    lblTime.Text = "§9:00";
                }
            }
            else
            {
                if (dStartTime > dTmp.AddHours(12) && dStartTime < dTmp.AddHours(24))
                {
                    string strStartTime = dStartTime.ToShortTimeString();
                    lblTime.Text = "§" + strStartTime + "";
                }
                else
                {
                    lblTime.Text = "§12:00";
                }
            }
        }
    }
    protected string PopForm(bool isAdd, string paras)
    {
        string strTitle = "修改日程";
        if (isAdd)
        {
            strTitle = "增加日程";
        }

        
        return JSUtil.ShowForm(null,strTitle, null, GlobalVar.WebCurrPath + "Edit.aspx" + paras, 400, 320);
       
    }

    protected string EditCal(object id)
    {
        string paras = "?ID=" + StrUtil.EncryptUrl(id.ToString());
        return PopForm(false, paras);
    }

    protected void drpDepts_SelectedIndexChanged(object sender, EventArgs e)
    {
        DeptBO dept = new DeptBO();
        IEnumerable<UserInfo> users = null;
        if (drpDepts.SelectedValue == "all")
        {
            users = dept.GetUserList((int)dept.GetRootID());
        }
        else
        {
            users = dept.GetUserList(int.Parse(drpDepts.SelectedValue));
        }
        CalendarBO.DeptUserSelect.BindUsers(drpUsers, users);
        BindView();
    }
    protected void drpUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
    }
    protected void btnPre_Click(object sender, ImageClickEventArgs e)
    {
        CurrentTime = CurrentTime.AddDays(-7);     
        AddAttr();
        BindView();
    }
    protected void btnNext_Click(object sender, ImageClickEventArgs e)
    {
        CurrentTime = CurrentTime.AddDays(7);     
        AddAttr();
        BindView();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }
    
}
